<?
/**
 * Grabli for q_id
 */

?>
<?php if(isset($this->filters) && is_array($this->filters)):?>
    <div class="all-filters-box corner-all">
        <div class="sidebar-caption">Уточнить поиск</div>
    	<?php foreach ($this->filters as $filterCaption=>$filterData):?>
        <?php 
            /**
             * @todo Refactor
             */
            $this->filterCaption = $filterCaption;
            $this->filterData = $filterData;
        ?>
    		<?php if(count($filterData)>1): //ne vivodim odino4nie?>
        

<dl class="filter" id="filter_<?= $this->filterCaption ?>">
	<?php if($this->additionalCaption):?>
	<dt> <?php 
	    // @todo Make additional filters
	?>
          <span class="currency">
              <a class="active left-item corner-left gradient gradient-dark-grey" href="search-filters.phtml#">$</a>
              <a class="center-item gradient gradient-grey" href="search-filters.phtml#">€</a>
              <a class="right-item corner-right gradient gradient-grey" href="search-filters.phtml#">грн</a>
          </span>
          <span><?= $this->filterCaption?></span>
    </dt>
	<?php else:?>
	<?php 
	    /**
	     * @todo Remove this WTF!!!
	     */
	    $params = array('q_id'=>$this->q_id);
		$additionalLink = null;
		//показувати тільки 4 штуки
		$i = 0; $max_element = 4;
		$hiden_element_count = 0;
						
	    $this->filterHeader = $this->getName($this->filterCaption);
	 ?>
	<dt><?= $this->filterHeader?></dt>
	<?php endif;?>
	<?php $additionalLink = null;?>
	<?php
        if ($this->filterName == 'exchangeTypeId') $this->filterName = 'exchange';
        $delim = '';
        //костиль
        $current_url = htmlentities($_SERVER["REQUEST_URI"]);

        $current_url = preg_replace('|&amp;page=[\d]{1,}|', '', $current_url);

        if (!strpos($current_url, '?')) {
            $delim = '?';
        }
    ?>
        <?php foreach ($this->filterData as $filterDataRow):?>
            <?php 
            if($filterDataRow['name'] == -1){
                $additionalLink = $filterDataRow;
                continue;
            }?>
        <dd  <?php if($i > $max_element): echo 'style = "display:none;"';$hiden_element_count++; endif;$i++;?> >
            <input type="checkbox" name="<?= $this->filterCaption?>" class="<?= $this->filterCaption?>" value="<?php if(isset($filterDataRow['id']))echo $filterDataRow['id'];else echo $filterDataRow['name'];?>" />
            
            <?php if(isset($filterDataRow['id'])):?>
                <? $params[$this->filterCaption]=$filterDataRow['id']; ?>
                <a href="<?=$this->qurl($params); ?>">
            <?php elseif('years' == $this->filterCaption):?>
                <? $params['year']=$filterDataRow['name']; ?>
                <a href="<?=$this->qurl($params); ?>">
			<?php elseif('price' == $this->filterCaption):?>
			
            <?php endif;?>
            
            	<?php if(isset($filterDataRow['from']) && isset($filterDataRow['to'])):?>
					<?php if(!$filterDataRow['from']):?>
						<?= _('До'). " " . $filterDataRow['to'];?>
					<?php else:?>
						<?= $filterDataRow['from'] . " - " . $filterDataRow['to'];?>
					<?php endif;?>
				<?php else: ?>
					<?= $filterDataRow['name'];?>
				<?php endif;?>
            </a> 
            (<?= $filterDataRow['count']?>)
            
        </dd>     
        <?php endforeach;?>
  
        <?php /*  if(null !== $additionalLink):?>
    		<dd>
                <span class="filter-box">
                    <span class="filter-box-content">
                        <span class="corner-left filter-box-content-left"><?= $additionalLink['count']?></span>
                        <span class="corner-right filter-box-content-right gradient gradient-grey">
                            <a class="corner-right icons-16 icon-16-remove" href=""></a>
                        </span>
                    </span>
                    <span class="depression"></span>
                 </span> 
                <a class="other icons-arrows icon-arrow-down" href="#"><span><?= _('Не указано');?></span></a>
            </dd>
        <?php endif; $additionalLink = null;*/?>
                                                 
		<?php if($hiden_element_count>0):?>                                                   
         <dd>
            <a class="other arrow-down" href="search-filters.phtml####" onclick="showUrls('<?=$this->filterCaption ?>', this);return false;"><span>Другие (<?= $hiden_element_count?>)</span></a>
        </dd>
        <? endif;?>
        
  	<dd class="dell-add-item" style="display:none;">
            Выбраные
            <a class="del-item corner-left gradient gradient-red"  href="search-filters.phtml#" onclick="return gotoUrl('<?= $this->filterCaption?>', 'not_');">Удалить</a>
            <a class="leave-item corner-right gradient gradient-black" href="search-filters.phtml#" onclick="return gotoUrl('<?= $this->filterCaption?>', '');">Оставить</a>
        </dd>
 </dl>

<script type="text/javascript">
var gotoUrl = function(name, comparison){
	
	var inputs = $("input."+name+":checkbox:checked");
	var url='';

	if(name == 'years'){name='year';}
	
	$.each(inputs, function(index, input) {
		url+='&amp;'+comparison+name+'[]='+input.value;
	});

    location.href = location.href + url;
	return false;
};

var showUrls = function (filterName, element){
	$("#filter_"+filterName+" dd[class!='dell-add-item']").css("display","block");
	$(element).css("display","none");
};
</script>

 <?php endif; ?>
    	<?php endforeach;?>
    </div>
<?php endif;?>